﻿namespace com.stifel.BranchMgmt.WebParts.Search
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;

    using com.stifel.BranchMgmtDao;
    using com.stifel.Extensions.ObjectExtensions;
    using com.stifel.Extensions.StringExtensions;

    using Microsoft.SharePoint;

    public partial class SearchUserControl
    {
        #region Properties

        public string TargetPage
        {
            get;
            set;
        }

        #endregion Properties

        #region Methods

        List<BranchInfoProperties> getBranchInfoProperties( List<StifelBranchInfo> l )
        {
            List<BranchInfoProperties> bipList = new List<BranchInfoProperties>( );
            if ( ( l != null ) && ( l.Count != 0 ) )
            {
                foreach ( StifelBranchInfo sbi in l )
                {
                    BranchInfoProperties bip = new BranchInfoProperties( );
                    bip.AddressAsHtml = sbi.AddressAsHtml;
                    //bip.AddressBranchCity = sbi.BranchCity;
                    //bip.AddressBranchState = sbi.USStatesLookupusr_us_value;
                    //bip.AddressBranchStreet = sbi.BranchStreetAddress;
                    //bip.AddressBranchSuite = sbi.BranchSuiteAddress;
                    bip.BranchLoactionId = sbi.BranchLocationIdentifier;
                    bip.BranchTitle = sbi.Title;
                    if ( sbi.Id.HasValue )
                    {
                        StifelBranchUnitDao dao = new StifelBranchUnitDao( SPContext.Current.Web.Url );
                        List<StifelBranchUnit> branchUnitList = dao.GetByBranchLocation( sbi.Id.Value );
                        List<BranchUnitProperties> bupList = new List<BranchUnitProperties>( );
                        foreach ( StifelBranchUnit sbu in branchUnitList )
                        {
                            BranchUnitProperties bup = new BranchUnitProperties( );
                            bup.BranchUnit = sbu.Title;
                            bup.CheckCodesAsHtml = sbu.CheckCodesAsHtml;
                            bup.DestinationCodesAsHtml = sbu.DestinationCodesAsHtml;
                            bup.WireCodesAsHtml = sbu.WireCodesAsHtml;
                            bup.OrgCode = sbu.ORG;
                            bupList.Add( bup );
                        }
                        bip.BranchUnits = bupList;
                    }
                    bip.BroadCastIpAsHtml = sbi.BroadCastIPAsHtml;
                    bip.ItemGuid = sbi.UniqueId;
                    bip.PriceServer = sbi.PriceServer;
                    bip.SiteId = sbi.SiteId;
                    bip.SpeedDial = sbi.SpeedDial;
                    bip.ThompsonIdPrefix = sbi.ThompsonIdPrefix;
                    bipList.Add( bip );
                }
            }
            return ( bipList );
        }

        List<ResultsInfo> getResultsForBinding( List<BranchInfoProperties> branchInfoProperties )
        {
            List<ResultsInfo> rList = new List<ResultsInfo>( );
            if ( ( branchInfoProperties != null ) && ( branchInfoProperties.Count != 0 ) )
            {
                foreach ( BranchInfoProperties bip in branchInfoProperties )
                {
                    ResultsInfo r = new ResultsInfo( );
                    r.Address = bip.AddressAsHtml;
                    r.ItemGuid = bip.ItemGuid.ToString( );
                    r.CompleteResultInfo = bip.SerializeToXml<BranchInfoProperties>( ).ToBase64String( );
                    r.TargetPage = TargetPage.Append( "?ItemGuid=" + bip.ItemGuid.ToString( ) );
                    rList.Add( r );
                }
            }
            return ( rList );
        }

        void hideMsgShowResults( )
        {
            uiResults.Visible = true;
            uiSearchMsg.Visible = false;
        }

        void hideRepeaterAndErrorMsg()
        {
            uiResults.Visible = false;
            uiSearchMsg.Visible = false;
        }

        void hideResultsShowMsg( )
        {
            uiResults.Visible = false;
            uiSearchMsg.Visible = true;
        }

        void navigateToSelectedView( Button button )
        {
            string arg = button.CommandArgument;
            int index = -1;

            for ( int i = 0; i < uiView.Views.Count; i++ )
            {
                View view = uiView.Views[ i ];
                if ( view.ID.Equals( arg, StringComparison.OrdinalIgnoreCase ) )
                {
                    index = i;
                    break;
                }
            }
            uiView.ActiveViewIndex = index >= 0 ? index : uiView.Views.Count - 1;
            foreach ( Control c in uiNavigation.Controls )
            {
                Button b = c as Button;
                if ( b != null )
                {
                    b.Attributes.Remove( Attribute.Current );
                }
            }
            button.Attributes.Add( Attribute.Current, true.ToString( ) );
        }

        void setBranchBasicInfo( PlaceHolder uiPlaceHolderBranchBasicInfo, BranchInfoProperties bip )
        {
            Table t = new Table( );
            TableRow r;
            TableCell c1;
            TableCell c2;
            if ( !string.IsNullOrEmpty( bip.BranchLoactionId ) )
            {
                r = new TableRow( );
                c1 = new TableCell( );
                c1.ColumnSpan = 2;
                c1.Controls.Add( new Literal
                {
                    Text = "Branch Id : " + bip.BranchLoactionId.Trim( )
                } );
                r.Cells.Add( c1 );
                t.Rows.Add( r );
            }
            if ( !string.IsNullOrEmpty( bip.SiteId ) )
            {
                r = new TableRow( );
                c1 = new TableCell( );
                c1.ColumnSpan = 2;
                c1.Controls.Add( new Literal
                {
                    Text = "Site Id : " + bip.SiteId.Trim( )
                } );
                r.Cells.Add( c1 );
                t.Rows.Add( r );
            }
            if ( !string.IsNullOrEmpty( bip.SpeedDial ) )
            {
                r = new TableRow( );
                c1 = new TableCell( );
                c1.ColumnSpan = 2;
                c1.Controls.Add( new Literal
                {
                    Text = "Speed Dial : " + bip.SpeedDial.Trim( )
                } );
                r.Cells.Add( c1 );
                t.Rows.Add( r );
            }
            if ( !string.IsNullOrEmpty( bip.ThompsonIdPrefix ) )
            {
                r = new TableRow( );
                c1 = new TableCell( );
                c1.ColumnSpan = 2;
                c1.Controls.Add( new Literal
                {
                    Text = "Thompson Id : " + bip.ThompsonIdPrefix.Trim( )
                } );
                r.Cells.Add( c1 );
                t.Rows.Add( r );
            }
            if ( !string.IsNullOrEmpty( bip.BroadCastIpAsHtml ) )
            {
                r = new TableRow( );
                c1 = new TableCell( );
                c2 = new TableCell( );
                c1.VerticalAlign = VerticalAlign.Top;
                c2.VerticalAlign = VerticalAlign.Top;
                c1.Controls.Add( new Literal
                {
                    Text = "BroadCast IP : "
                } );
                c2.Controls.Add( new Literal
                {
                    Text = bip.BroadCastIpAsHtml.Trim( )
                } );
                r.Cells.Add( c1 );
                r.Cells.Add( c2 );
                t.Rows.Add( r );
            }
            if ( !string.IsNullOrEmpty( bip.PriceServer ) )
            {
                r = new TableRow( );
                c1 = new TableCell( );
                c1.ColumnSpan = 2;
                c1.Controls.Add( new Literal
                {
                    Text = "Price Server : " + bip.PriceServer.Trim( )
                } );
                r.Cells.Add( c1 );
                t.Rows.Add( r );
            }
            uiPlaceHolderBranchBasicInfo.Controls.Add( t );
        }

        void setBranchUnitInfo( PlaceHolder uiPlaceHolderUnit, BranchInfoProperties bip )
        {
            if ( ( bip.BranchUnits != null ) && ( bip.BranchUnits.Count != 0 ) )
            {
                foreach ( BranchUnitProperties bup in bip.BranchUnits )
                {
                    Table t = new Table( );
                    TableRow r;
                    TableCell c1;
                    TableCell c2;
                    TableCell c3;
                    if ( !string.IsNullOrEmpty( bup.BranchUnit ) )
                    {
                        r = new TableRow( );
                        c1 = new TableCell( );
                        c1.ColumnSpan = 3;
                        c1.Controls.Add( new Literal
                        {
                            Text = bup.BranchUnit.Trim( )
                        } );
                        r.Cells.Add( c1 );
                        t.Rows.Add( r );
                    }
                    if ( !string.IsNullOrEmpty( bup.OrgCode ) )
                    {
                        r = new TableRow( );
                        c1 = new TableCell( );
                        c1.ColumnSpan = 2;
                        c3 = new TableCell( );
                        c3.Controls.Add( new Literal
                        {
                            Text = "&nbsp;&nbsp;&nbsp;&nbsp;"
                        } );
                        r.Cells.Add( c3 );
                        c1.Controls.Add( new Literal
                        {
                            Text = "Org Code : " + bup.OrgCode.Trim( )
                        } );
                        r.Cells.Add( c1 );
                        t.Rows.Add( r );
                    }
                    if ( !string.IsNullOrEmpty( bup.CheckCodesAsHtml ) )
                    {
                        r = new TableRow( );
                        c1 = new TableCell( );
                        c2 = new TableCell( );
                        c3 = new TableCell( );
                        c1.VerticalAlign = VerticalAlign.Top;
                        c2.VerticalAlign = VerticalAlign.Top;
                        c3.Controls.Add( new Literal
                        {
                            Text = "&nbsp;&nbsp;&nbsp;&nbsp;"
                        } );
                        r.Cells.Add( c3 );
                        c1.Controls.Add( new Literal
                        {
                            Text = "Check Code(s) : "
                        } );
                        c2.Controls.Add( new Literal
                        {
                            Text = bup.CheckCodesAsHtml.Trim( )
                        } );
                        r.Cells.Add( c1 );
                        r.Cells.Add( c2 );
                        t.Rows.Add( r );
                    }
                    if ( !string.IsNullOrEmpty( bup.WireCodesAsHtml ) )
                    {
                        r = new TableRow( );
                        c1 = new TableCell( );
                        c2 = new TableCell( );
                        c1.VerticalAlign = VerticalAlign.Top;
                        c2.VerticalAlign = VerticalAlign.Top;
                        c3 = new TableCell( );
                        c3.Controls.Add( new Literal
                        {
                            Text = "&nbsp;&nbsp;&nbsp;&nbsp;"
                        } );
                        r.Cells.Add( c3 );
                        c1.Controls.Add( new Literal
                        {
                            Text = "Wire Code(s) : "
                        } );
                        c2.Controls.Add( new Literal
                        {
                            Text = bup.WireCodesAsHtml.Trim( )
                        } );
                        r.Cells.Add( c1 );
                        r.Cells.Add( c2 );
                        t.Rows.Add( r );
                    }
                    if ( !string.IsNullOrEmpty( bup.DestinationCodesAsHtml ) )
                    {
                        r = new TableRow( );
                        c1 = new TableCell( );
                        c2 = new TableCell( );
                        c1.VerticalAlign = VerticalAlign.Top;
                        c2.VerticalAlign = VerticalAlign.Top;
                        c3 = new TableCell( );
                        c3.Controls.Add( new Literal
                        {
                            Text = "&nbsp;&nbsp;&nbsp;&nbsp;"
                        } );
                        r.Cells.Add( c3 );
                        c1.Controls.Add( new Literal
                        {
                            Text = "Destination Code(s) : "
                        } );
                        c2.Controls.Add( new Literal
                        {
                            Text = bup.DestinationCodesAsHtml.Trim( )
                        } );
                        r.Cells.Add( c1 );
                        r.Cells.Add( c2 );
                        t.Rows.Add( r );
                    }
                    uiPlaceHolderUnit.Controls.Add( t );
                }
            }
        }

        void setDefaultView( )
        {
            Button button = uiNavigation.FindControl( "uiBtnBranchId" ) as Button;
            navigateToSelectedView( button );
        }

        bool isNotPromoting( Guid uniqueId )
        {
            StifelBranchPromotionWorklistDao dao = new StifelBranchPromotionWorklistDao( SPContext.Current.Web.Url );
            StifelBranchPromotionWorklist o = dao.GetByTitle( uniqueId );
            if ( o == null )
                return true;
            else
            {
                DateTime a = DateTime.Now;
                DateTime b = o.Modified.AddMinutes( 15 );
                return a.CompareTo( b ) > 0;
            }
        }

        bool isValidRequest( Guid uniqueId )
        {
            StifelBranchInfoDao dao = new StifelBranchInfoDao( SPContext.Current.Web.Url );
            StifelBranchInfo o = dao.Get( uniqueId );
            return o == null ? false : true;
        }

        #endregion Methods

        #region Nested Types

        class Attribute
        {
            #region Fields

            public const string Current = "current";

            #endregion Fields
        }

        class TextAlign
        {
            #region Fields

            public const string left = "left";
            public const string right = "right";
            public const string rightNtop = "rightNtop";
            public const string top = "top";

            #endregion Fields
        }

        #endregion Nested Types
    }
}